/*
*************************************************
				Copyagent
	------------------------------------------
	            html_parser.c
*************************************************
Parserul de script HTML
*/


/*
HTML parser

	<	a	' '	h	r	e	f	=	"	> Altceva
10	1	0	0	0	0	0	0	0	0	10	0
0	1	0	0	0	0	0	0	0	0	10	0		
1	1	2	1	1	1	1	1	1	1	10	1		
2	1	1	2	3	1	1	1	1	1	10	1
3	1	1	1	1	4	1	1	1	1	10	1
4	1	1	1	1	1	5	1	1	1	10	1
5	1	1	1	1	1	1	6	1	1	10	1
6	1	1	6	1	1	1	1	7	1	10	1
7	1	1	7	1	1	1	1	1	11	10	1
11	1	8	8	8	8	8	8	8	9	10	8	
8	1	8	8	8	8	8	8	8	9	10	8	
9	1	1	1	1	1	1	1	1	1	10	1
*/
#include <stdio.h>
#include <string.h>

#define BUFF_SIZE 4096
//US = unit separator
#define US 31

int stare = 0;
static int text_buffer_index = 0;
static int link_buffer_index = 0;

 short charWasSpace = 0;

char text_buffer[BUFF_SIZE];
char link_buffer[BUFF_SIZE];

void reset_indexes_and_buffers();
void adauga_litera(char arg);
void adauga_la_text(char arg);
void adauga_la_linkuri(char arg);
void FSM (char arg);

/*
void reset_indexes_and_buffers()
*/
void reset_indexes_and_buffers()
{
	text_buffer_index = 0;
	link_buffer_index = 0;
	memset(text_buffer, 0 , BUFF_SIZE);
	memset(link_buffer, 0 , BUFF_SIZE);
}

/*
void adauga_litera(char arg)
-------------------------------------------------
Adauga la text_buffer caracterul, doar daca este in range-ul 32-127 ASCII
-------------------------------------------------
char arg:	litera de adaugat
*/
void adauga_litera(char arg)
{
	if ((arg > 31) && (arg < 127))
	{
		//printf ("text_buffer[%d] = %c - %d [%d]\n",
		//	text_buffer_index, arg, arg, charWasSpace);
		text_buffer[text_buffer_index++] = arg;
	}
}

/*
void adauga_la_text(char arg);
-------------------------------------------------
Adauga caracterul la bufferul ce contine propozitii
-------------------------------------------------
char arg:	litera ce se va adauga la buffer
*/
void adauga_la_text(char arg)
{
	if ((charWasSpace == 1) && (arg != 32))
		adauga_litera(arg);
		
	if (charWasSpace == 0)
	{
		adauga_litera(arg);
	}
	
	if ((arg <= 32 ) || (arg >= 127))
		charWasSpace = 1;
	else
		charWasSpace = 0;
}

/*
void link_la_text(char arg);
-------------------------------------------------
Adauga caracterul la bufferul ce contine linkuri
-------------------------------------------------
char arg:	litera ce se va adauga la buffer
*/
void adauga_la_linkuri(char arg)
{
	//printf ("link_buffer[%d] = %c \n",link_buffer_index, arg);
	link_buffer[link_buffer_index++] = arg;
}


/*
void FSM (char arg)
-------------------------------------------------
Masina de stari a parserului de HTML
-------------------------------------------------
*/
void FSM (char arg)
{
	switch(arg)
	{
		case '<':
		{
			if ((stare == 0) || (stare == 10))
				stare = 1;
			break;
		}
		case 'a':
		{
			if (stare == 1)
				stare = 2;
			else 
				if (stare == 0)
					stare = 0;
				else
					if ((stare == 8) || (stare == 11))
						stare = 8;
					else
						stare = 1;
					
			break;
		}
		case ' ':
		{
			if ((stare == 0) || (stare == 10))
				stare = 0;
			else 
				if (stare == 2)
					stare = 2;
				else 
					if (stare == 6)
						stare = 6;
					else
						if (stare == 7)
							stare = 7;
						else
							if ((stare == 8) || (stare == 11))
								stare = 8;
							else
								stare = 1;
				
			break;
		}
		case 'h':
		{
			if ((stare == 0) || (stare == 10))
				stare = 0;
			else
				if (stare == 2)
					stare = 3;
				else
					if ((stare == 8) || (stare == 11))
						stare = 8;
					else 
						stare = 1;
		
			break;
		}
		case 'r':
		{
			if ((stare == 0) || (stare == 10))
					stare = 0;
				else
					if (stare == 3)
						stare = 4;
					else
						if ((stare == 8) || (stare == 11))
							stare = 8;
						else 
							stare = 1;
			break;
		}
		case 'e':
		{
			if ((stare == 0) || (stare == 10))
					stare = 0;
				else
					if (stare == 4)
						stare = 5;
					else
						if ((stare == 8) || (stare == 11))
							stare = 8;
						else 
							stare = 1;
			break;
		}
		case 'f':
		{
			if ((stare == 0) || (stare == 10))
					stare = 0;
				else
					if (stare == 5)
						stare = 6;
					else
						if ((stare == 8) || (stare == 11))
							stare = 8;
						else 
							stare = 1;
			break;
		}
		case '=':
		{
			if ((stare == 0) || (stare == 10))
					stare = 0;
				else
					if (stare == 6)
						stare = 7;
					else
						if ((stare == 8) || (stare == 11))
							stare = 8;
						else 
							stare = 1;
			break;
		}
		case '"':
		{
			if ((stare == 0) || (stare == 10))
					stare = 0;
				else
					if (stare == 7)
						stare = 11;
					else
						if ((stare == 8) || (stare == 11))
							stare = 9;
						else 
							stare = 1;
			break;
		}
		case '>':
			stare = 10;
			break;
		default:
		{
			if ((stare == 0) || (stare == 10))
					stare = 0;
				else
					if ((stare == 8) || (stare == 11))
						stare = 8;
					else 
						stare = 1;
			break;
		}
	
	}
	
	if (stare == 0)
		adauga_la_text(arg);
	else
		if (stare == 8)
			adauga_la_linkuri(arg);
		else
			if (stare == 10)
			{
				//Separ propozitiile
				text_buffer[text_buffer_index++] = US;
			}
			else
				if (stare == 11)
					{
						//Separ linkurile
						link_buffer[link_buffer_index++] = US;
					}
}
